var wxRequest = require('../../utils/wxRequest')
var Common = require('../../utils/common');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payInfo:null,
    billIdStr: "",
    idRoom: 0,
    nameCommunity: "",
    nameArea: "",
    nameFloor: "",
    nameRoom:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      billIdStr: options.billIdStr,
      idRoom: options.idRoom,
      nameCommunity: options.nameCommunity,
      nameArea: options.nameArea,
      nameFloor: options.nameFloor,
      nameRoom: options.nameRoom,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  //  this.onLoad();
   var that = this;
    var url = app.globalData.domain + "/client/propertyBill/payment";
    var parm = {
      token: wx.getStorageSync("token"),
      idRoom: that.data.idRoom,
      ids: that.data.billIdStr
    }
    wxRequest.postRequest(url, parm).then(result => {
      console.log("立刻缴费", result);
      that.setData({
        payInfo: result.data
      });

      
    });
  },
  //发起微信支付
  requestPayment: function (e) {
    var that = this;
    console.log(e);
    Common.showLoading("加载中");
    var url = app.globalData.domain + "/client/propertyBill/pay";
    var parm = {
      token: wx.getStorageSync("token"),
      payOrderNo: e.detail.value.payOrderNo
    }
    console.log("请求url=>", url);
    wxRequest.postRequest(url, parm, wx.getStorageSync("token")).then(res => {
      console.log("支付结果=>", res);
      Common.hideLoading(); 
      wx.requestPayment({
        timeStamp: String(res.data.timeStamp),
        nonceStr: res.data.nonce_str,
        package: res.data.prepay_id,
        signType: "MD5",
        paySign: res.data.sign,
        success(res) {
          console.log("支付调用结果=>", res);
          Common.showSuccessToast("支付成功");
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/bill_pay_success/bill_pay_success'
            })
          }, 1000)
        },
        fail(res) {
          console.log("支付调用结果 fail=>", res);
          Common.showToast("支付取消");
        }
      });

    }, error => {
      Common.hideLoading();
      Common.showToast(error);
    });
    


  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})